package com.aizhixin.lab.post.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.post.data.SkillPointsData;
import com.aizhixin.lab.post.domain.PostSkillDomain;
import com.aizhixin.lab.post.domain.SkillColorDomain;
import com.aizhixin.lab.post.server.SkillColorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/post/skillColor")
@Api(value = "熟练度模块", description = "熟练度模块")
public class SkillColorController {

    @Autowired
    private SkillColorService colorService;
    @Autowired
    private AccountService accountService;
    @RequestMapping(value = "/findAllColor", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取所有熟练度信息", httpMethod = "GET", notes = "获取所有熟练度信息<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = SkillColorDomain.class)})
    public ResponseEntity<?> findAllColor(@RequestHeader("Authorization") String token) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            List<SkillColorDomain> allDomain = colorService.findAllDomain();
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,allDomain);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除", httpMethod = "DELETE", notes = "删除<br><br><b>lwq</b>")
    public ResponseEntity<?> findModuleList(@RequestHeader("Authorization") String token,@RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            colorService.delete(id);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,"删除成功");
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增", httpMethod = "POST", notes = "新增<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token,@RequestBody SkillColorDomain  domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            colorService.saveDomain(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,"添加成功");
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改", httpMethod = "POST", notes = "修改<br><br><b>lwq</b>")
    public ResponseEntity<?> update(@RequestHeader("Authorization") String token,@RequestBody SkillColorDomain  domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            colorService.updateDomain(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,"修改成功");
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

}
